//
//  WDCalendarCollectionView.m
//  MyDemos
//
//  Created by iMac on 2018/3/6.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "WDCalendarCollectionView.h"
#import "WDCalendarMonthView.h"
#import "WDCalendarDateHandler.h"
#import "WDCalendarDayModel.h"




@interface WDCalendarCollectionView() <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSArray *calendarModelsGroup;

@end

@implementation WDCalendarCollectionView

- (instancetype)initWithFrame:(CGRect)frame {
    UICollectionViewFlowLayout *flowLayout = ({
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemWidth = SCREEN_WIDTH / 7.0;
        WDCalendarDateHandler *handle = [WDCalendarDateHandler sharedInstance];
        layout.itemSize = CGSizeMake(SCREEN_WIDTH, itemWidth * [handle numsOfRows:[NSDate date]]);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout;
    });
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    if (self) {
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"WDCalendarCellID"];
        self.calendarModelsGroup = [[WDCalendarDateHandler sharedInstance] dataForCalendar:[NSDate date]];
        
    }
    return self;
}



- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WDCalendarCellID" forIndexPath:indexPath];
    WDCalendarDateHandler *handler = [WDCalendarDateHandler sharedInstance];
    WDCalendarDayModel *model = self.calendarModelsGroup[indexPath.item][0];
    NSInteger row = [handler numsOfRows:model.date];
    WDCalendarMonthView *month = [[WDCalendarMonthView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_WIDTH / 7.0) * row)];
    month.datas = self.calendarModelsGroup[indexPath.item];
    [cell addSubview:month];
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.calendarModelsGroup.count;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"------------   %f", scrollView.contentOffset.x);
    WDCalendarDayModel *model = self.calendarModelsGroup[1][0];
    NSDate *date = model.date;
    NSLog(@"处理前 date:%@" ,date);
    WDCalendarDateHandler *handler = [WDCalendarDateHandler sharedInstance];
    if (scrollView.contentOffset.x == 0) {
        date = [handler dateForLastMonth:date];
    }
    else if (scrollView.contentOffset.x == SCREEN_WIDTH) {
        
    }
    else if (scrollView.contentOffset.x == SCREEN_WIDTH * 2) {
        date = [handler dateForNextMonth:date];
    }
    NSLog(@"处理后 date:%@" ,date);
    self.calendarModelsGroup = [handler dataForCalendar:date];
}

- (void)setCalendarModelsGroup:(NSArray *)calendarModelsGroup {
    _calendarModelsGroup = calendarModelsGroup;
    [self reloadData];
    WDCalendarDayModel *model = calendarModelsGroup[1][0];
    NSInteger row = [[WDCalendarDateHandler sharedInstance] numsOfRows:model.date];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_WIDTH / 7.0) * row);
    [self layoutSubviews];
    [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

@end
